<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Logic\RedbillLogic;
use App\Logic\UserLogic;
use App\Middleware\AuthMiddleware;
use App\Utils\Enum\MatchEnum;
use App\Utils\Enum\RedbillEnum;
use App\Utils\Enum\UserEnum;
use Exception;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Psr\Http\Message\ResponseInterface;

#[AutoController]
class RedbillController extends AbstractController
{
    /**
     * 获取专家详情
     * @return ResponseInterface
     */
    public function getExpertDetail(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $expertUserId = $this->request->input('expertUserId', 0);
        if (!$expertUserId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = RedbillLogic::getExpertDetail($userid, $expertUserId);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }

        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 获取方案列表.
     */
    public function getProjectList(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $input = $this->request->all();
        $data = RedbillLogic::getProjectList($userid, $input);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 获取方案过滤可选的赛事列表
     * @return ResponseInterface
     */
    public function getProjectFilterGames(): ResponseInterface
    {
        $sportId = $this->request->input('sportId', 0);
        if (!$sportId || !in_array($sportId, [MatchEnum::SPORT_BASKETBALL, MatchEnum::SPORT_FOOTBALL])) {
            return $this->apiReturn(400, '参数错误');
        }
        $games = RedbillLogic::getFilterGames($sportId);
        return $this->apiReturn(200, 'succ', $games);
    }

    /**
     * 获取方案详情.
     */
    public function getProjectInfo(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $projectId = $this->request->input('projectId', '');
        if (empty($projectId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $info = RedbillLogic::getProjectDetail($userid, $projectId);
            return $this->apiReturn(200, 'succ', $info);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 发布方案.
     */
    #[Middleware(AuthMiddleware::class)]
    public function publishProject(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $input = $this->request->all();
        if (empty($input['matchId']) || !intval($input['playTypeCode']) || !is_array($input['suggestResultCode']) || empty($input['suggestResultCode']) || !intval($input['priceId'])) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $info = RedbillLogic::addProject($userid, $input);
            return $this->apiReturn(200, '发布成功', $info);
        } catch (Exception $e) {
            echo $e->getFile() . "\n";
            echo $e->getLine() . "\n";
            echo $e->getCode() . "\n";
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 发布方案时获取相关选项
     */
    #[Middleware(AuthMiddleware::class)]
    public function loadReleaseRelationInfo(): ResponseInterface
    {
        $sportId = $this->request->input('sportId');
        $data = RedbillLogic::getReleaseRelationInfo($sportId);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 购买方案.
     */
    #[Middleware(AuthMiddleware::class)]
    public function buyProject(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $projectId = $this->request->input('projectId', '');
        if (empty($projectId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            RedbillLogic::buyProject($userid, $projectId, $input['couponId'] ?? 0);
            return $this->apiReturn(200, '购买成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取优惠券清单.
     */
    #[Middleware(AuthMiddleware::class)]
    public function loadCouponList(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $data = UserLogic::getCouponList($userid, $this->request->all());
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 我发布的方案清单.
     */
    #[Middleware(AuthMiddleware::class)]
    public function myProjectList(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $data = RedbillLogic::getMyReleaseProjectList($userid, $this->request->all());
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 我购买的方案记录.
     */
    #[Middleware(AuthMiddleware::class)]
    public function myPaidProjectList(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $data = RedbillLogic::getMyPaidProjectList($userid, $this->request->all());
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 获取热门专家列表.
     */
    public function getHotExpertList(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        try {
            $data = RedbillLogic::getRankList($userid, RedbillEnum::RANK_TYPE_FANS, 10);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取排行版.
     */
    public function getRankList(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $rankType = $this->request->input('rankType', 0);
        $topNumDefault = match ($rankType) {
            RedbillEnum::RANK_TYPE_FANS => 100,
            RedbillEnum::RANK_TYPE_RED => 20,
            RedbillEnum::RANK_TYPE_HIT => 200,
            RedbillEnum::RANK_TYPE_BACK => 200,
            default => 0
        };
        $topNum = $this->request->input('topNum', $topNumDefault);
        if (!$topNum) {
            return $this->apiReturn(400, '不支持该类型排行榜');
        }
        try {
            $data = RedbillLogic::getRankList($userid, $rankType, $topNum);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 关注/取关专家.
     */
    #[Middleware(AuthMiddleware::class)]
    public function follow(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $expertUserId = $this->request->input('expertUserId', 0);

        if (!$expertUserId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $followed = RedbillLogic::follow($userid, $expertUserId);
            return $this->apiReturn(200, '操作成功', ['followed' => $followed]);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取最新入驻记录
     * @return ResponseInterface
     */
    #[Middleware(AuthMiddleware::class)]
    public function settledResult(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $result = RedbillLogic::getLastSettledResult($userid);
        return $this->apiReturn(200, 'succ', $result);
    }


    /**
     * 入驻申请
     * @return ResponseInterface
     */
    #[Middleware(AuthMiddleware::class)]
    public function settled(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $roleId = Context::get('roleId', 0);
        if ($roleId != UserEnum::USER_TYPE_NORMAL) {
            return $this->apiReturn(400, '当前身份不可入驻专家');
        }
        $input = $this->request->all();
        if (empty($input['contact']) || empty($input['telephone']) || empty($input['introduce'])) {
            return $this->apiReturn(400, '请认真完整填写申请表单');
        }
        try {
            $result = RedbillLogic::settled($userid, $input);
            return $this->apiReturn(200, '提交成功', $result);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }
}
